package com.zmzncs.shop.module.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.shop.common.pojo.AuthorizeVO;
import com.zmzncs.shop.common.pojo.PageInfo;
import com.zmzncs.shop.common.pojo.Rest;
import com.zmzncs.shop.common.util.JsonUtil;
import com.zmzncs.shop.common.util.RestUtil;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shopmanager.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zmzncs.shop.module.shop.service.ShopManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 高博文
 * @since 2021-05-25
 */
@Api(tags = {"商户-管理员"})
@RestController
@RequestMapping("/shop/shop-manager")
public class ShopManagerController {

    @Autowired
    private ShopManagerService shopManagerService;

    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Rest add(@RequestBody @Validated ShopManagerAddForm form){
        shopManagerService.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public Rest delete(@RequestParam Long id){
        shopManagerService.delete(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("update")
    public Rest update(@RequestBody @Validated ShopManagerUpdateForm form){
        shopManagerService.update(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Rest<PageInfo<ShopManagerVO>> page(@Valid ShopManagerSearchForm form){
        IPage<ShopManagerDTO> iPage = shopManagerService.page(form);
        PageInfo<ShopManagerVO> pageInfo = JsonUtil.pageTransform(iPage, ShopManagerVO.class);

        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "详情")
    @GetMapping("get-by-id")
    public Rest<ShopManagerVO> getById(@RequestParam Long id){
        ShopManagerDTO shopManagerDTO = shopManagerService.getById(id);

        ShopManagerVO shopManagerVO = new ShopManagerVO();
        BeanUtils.copyProperties(shopManagerDTO, shopManagerVO);

        return RestUtil.success(shopManagerVO);
    }


    @ApiOperation(value = "获取短信验证码")
    @GetMapping("get-verify-code")
    public Rest getVerifyCode(@RequestParam String phoneNumber){
        Map<String, Object> verifyCode = shopManagerService.sendVerifyCode(phoneNumber);

        return RestUtil.success(verifyCode);
    }

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public Rest<AuthorizeVO> login(@RequestBody @Valid LoginForm form){
        AuthorizeVO login = shopManagerService.login(form);

        return RestUtil.success(login);
    }

    @ApiOperation(value = "微信授权")
    @PostMapping(value = "/authorize")
    public Rest<ShopManagerDTO> authorize(@Validated WeiXinAuthorizeForm weiXinAuthorizeForm) {
        ShopManagerDTO parkManagerDTO = shopManagerService.authorize(weiXinAuthorizeForm);
        return RestUtil.success(parkManagerDTO);
    }




}
